﻿@using Model.Admin
@using Xproject.Util
@using Model.WXshopBuiness.Enum
@{
    string Domain = CommonHelper.GetAppSettings("Domin");
    //Layout = "~/Views/Shared/_JSlayout.cshtml";
    ViewBag.Title = "我的订单";
    Layout = "~/Views/Shared/_ShopLayout.cshtml";
}
 
    <style type="text/css">
        .smallfont {
            font-size: 1em;
        }
        .active {
            color: #246fc0;
            background-color: pink;
        }
        .textover {
            
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .yikanjia {
            color: yellow;
        }

        .yikanjiange {
            background-image: url("../../Content/Image/Promotions/KanJia/tf.png");
            width: 100%;
            height: 90%;
            background-size: 100% 90%;
            position: absolute;
        }
        .midline {
            text-decoration: line-through;
        }
        .redClass {
            color:red;
        }
         .col-xsm-2 {
            margin: 0.571em 0;
            padding: 0;
            border-right: 1px solid #ececec;
            text-align: center;           
            color: #666;
        }
    </style>
 

    <!--top-->
    <div class="top_c">
        <a href="~/UserCenter/UserCenter" class="iconfont icon-jiantou-copy-copy"></a>
        <p class="titi">我的订单</p>
    </div>

    <!--头部-->
    <div class="pos">
        <div class="container">
            <div class="row titll">
                <a href="@(Domain+"/order/MyOrders?type=all")"><div class="col-xs-2   @(ViewBag.ordertype=="all"?"active":"")" style="font-size:1.1em"><p>全部</p></div></a>
                <a href="@(Domain+"/order/MyOrders?type="+(int)Enum_OrderState.WAITINGPAY)"><div class="col-xs-2   @(ViewBag.ordertype=="1"?"active":"")" style="font-size:1.1em"><p  >待付款</p></div></a>
                <a href="@(Domain+"/order/MyOrders?type="+(int)Enum_OrderState.PAYED)"><div class="col-xs-2   @(ViewBag.ordertype=="5"?"active":"")" style="font-size:1.1em"><p >待发货</p></div></a>
                <a href="@(Domain+"/order/MyOrders?type="+(int)Enum_OrderState.SHIPPING)"><div class="col-xs-2   @(ViewBag.ordertype=="10"?"active":"")" style="font-size:1.1em"><p  >待收货</p></div></a>
                <a href="@(Domain+"/order/MyOrders?type="+(int)Enum_OrderState.WAITCOMMENT)">
                    <div class="col-xs-2   @(ViewBag.ordertype=="20"?"active":"")" style="font-size:1.1em">
                        <p  >待评价</p>
                    </div>
                </a>
                <a href="@(Domain+"/order/MyOrders?type="+(int)Enum_OrderState.FINISHED)"><div class="col-xs-2   @(ViewBag.ordertype=="30"?"active":"")" style="border-right:none;font-size:1.1em"><span  >已完成</span></div></a>
            </div>
        </div>
    </div>


    <!--列表-->

    <div id="wrapper">
        <div id="scroller">
            <div id="pullDown">
                <span class="pullDownIcon"></span>
                <span class="pullDownLabel">下拉刷新...</span>
            </div>
            <div class="ding_d" id="thelist" style="margin-top:0px">
            </div>
            <div id="pullUp" style="margin-top:0px">
                <span class="pullUpIcon"></span>
                <span class="pullUpLabel">上拉加载更多...</span>
            </div>
        </div>
    </div>
     
    <input type="hidden" id="hiddenType" value="@ViewBag.ordertype" />

  
    <script type="text/javascript">

            var myScroll,
                pullDownEl,
                pullDownOffset,
                pullUpEl,
                pullUpOffset;

            //todo 自己定义的变量
            var userDataSum,//存储数据的变量
                addNum = 0,
                stepLength = 8,//每次加载的步长
                ajaxIndex = 1,
                sumL = 0;//数据的长度
            /**
             *  刷新滚动区域的滚动条的位置（此方法在添加数据后调用）
             **/
            function refreshScrollBar() {
                console.log("刷新滚动条");
                setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
                    myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
                }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
            }

            function pullDownAction() {
                console.log("下拉");

                setTimeout(function () {
                    (function reloadPage() {
                        if (GetQueryString("_rf")) {
                            window.location.href = window.location.href.replace(GetQueryString("_rf"), Math.random());
                        } else {
                            window.location.href = window.location.href + (window.location.href.indexOf("?") > -1 ? "&" : "?") + "_rf=" + Math.random();
                        }
                    })();
                    myScroll.refresh();
                    console.log($("#thelist").height())
                }, 1000);
            }
            //初始化数据
            function pullUpAction() {
                console.log("上拉");

                setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!

                    getData(ajaxIndex, stepLength);
                    myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
                }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
            }

            function loaded() {
                pullDownEl = document.getElementById('pullDown');
                pullDownOffset = pullDownEl.offsetHeight;
                pullUpEl = document.getElementById('pullUp');
                pullUpOffset = pullUpEl.offsetHeight;
                myScroll = new iScroll('wrapper', {
                    useTransition: true,
                    topOffset: pullDownOffset,
                    onRefresh: function () {
                        if (pullDownEl.className.match('loading')) {
                            pullDownEl.className = '';
                            pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                        } else if (pullUpEl.className.match('loading')) {
                            pullUpEl.className = '';
                            pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                        }
                    },
                    onScrollMove: function () {
                        if (this.y > 5 && !pullDownEl.className.match('flip')) {
                            pullDownEl.className = 'flip';
                            pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
                            this.minScrollY = 0;
                        } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                            pullDownEl.className = '';
                            pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                            this.minScrollY = -pullDownOffset;
                        } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                            pullUpEl.className = 'flip';
                            pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
                            this.maxScrollY = this.maxScrollY;
                        } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                            pullUpEl.className = '';
                            pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                            this.maxScrollY = pullUpOffset;
                        }
                    },
                    onScrollEnd: function () {
                        if (pullDownEl.className.match('flip')) {
                            pullDownEl.className = 'loading';
                            pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';

                            refreshScrollBar();
                            pullDownAction();	// Execute custom function (ajax call?)
                        } else if (pullUpEl.className.match('flip')) {
                            pullUpEl.className = 'loading';
                            pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';
                            if (userDataSum!=0 ) {
                                pullUpAction();	// Execute custom function (ajax call?)

                            } else {
                                pullUpEl.querySelector('.pullUpLabel').innerHTML = "数据已全部加载完毕！";
                            }
                        }
                    }
                });

                setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
            }

            document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

            document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);

            function GetQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                return (r != null && r.length >= 2) ? decodeURI(r[2]) : null;
            }

            getData(ajaxIndex, stepLength);

            function getData(index, pagesize) {
                layer.open({ type: 2, shadeClose: false });
                var type = $("#hiddenType").val();
                $.ajax({
                    type: "get",
                    url: "@(ViewBag.DomainUrl + "/Order/partialMyOrderList")" + "?type=" + type+"&index=" + index + "&pagesize="+pagesize,
                    dataType: "json",
                    success: function (data) {
                        userDataSum = data.Data.length;
                        var datas = "";
                        if (userDataSum != 0) {
                            ajaxIndex++;
                        }
                            for (var i = 0; i < data.Data.length; i++) {
                                datas = datas + BuildDIViitem(data.Data[i]);
                        }
                            layer.closeAll('dialog');
                       $("#thelist").append(datas);
                    }
                })
            }

            function GetStatusText(state) {
                var text = "待支付";
                if (state == 1) {
                    text = "待支付";
                }
                if (state ==5) {
                    text = "已支付";
                }
                if (state == 10) {
                    text = "已发货";
                }
                if (state ==15) {
                    text = "已签收";
                }
                if (state == 20) {
                    text = "待评价";
                }
                if (state == 30) {
                    text = "已完成";
                }
                return text;
            }

            function DoComment() {

            }

            function BuildOper(orderId, state, HasKanJia, KanJiaID, StrAleradKanJiaMoney, PromotionID) {
                var text = '<a href="/Order/PayOrderDetail?orderId=' + orderId + '"  class="liji">立即付款</a>' +
                    ' <a href="#" onclick="DelThisOrder(\'' + orderId +'\')" class="qu">取消订单</a>';
                if (state == 1) {
                    text = '<a  href="/Order/PayOrderDetail?orderId=' + orderId + '" class="liji">立即付款</a><a href="#" onclick="DelThisOrder(\'' + orderId +'\')" class="qu">取消订单</a>';
                    if (HasKanJia == true) {
                        text = '<a class="yikanjiange" href="/Promotions/KJPromotions?KanJiaID=' + KanJiaID + '"   style="float:left;width:12rem;color:yellow"> 已砍价<label  class="yikanjia">￥' + StrAleradKanJiaMoney + '</label></a>'
                            + '<a  href="/Order/PayOrderDetail?orderId=' + orderId + '" class="liji">立即付款</a><a href="#" onclick="DelThisOrder(\'' + orderId + '\')"  class="qu">取消订单</a>';
                    } else {
                        if (PromotionID != null) {
                            text = '<a   onclick="addKanJiaFromShopCar(\'' + orderId + '\')"  style="float:left;width:8rem"><img  style="width: 12rem;height: 2.14rem;" src="/Content/Image/Promotions/KanJia/kanjia.png"/><span></span></a><a  href="/Order/PayOrderDetail?orderId=' + orderId + '" class="liji">去付款</a><a href="#" onclick="DelThisOrder(\'' + orderId + '\')"  class="qu">取消</a>';
                            + '<a  href="/Order/PayOrderDetail?orderId=' + orderId + '" class="liji">立即付款</a><a href="#" onclick="DelThisOrder(\'' + orderId + '\')"  class="qu">取消订单</a>';
                        } else {
                            text = '<a  href="/Order/PayOrderDetail?orderId=' + orderId + '" class="liji">立即付款</a><a href="#" onclick="DelThisOrder(\'' + orderId + '\')"  class="qu">取消订单</a>';
                        }
                        }
                }
                if (state ==5) {
                    text = '<a href="#" onclick="RejectOrder()" class="liji">退款退货</a><a href="#" onclick="ConfirmReceiptOrder(\'' + orderId+'\')" class="qu">确认收货</a>';
                }
                if (state == 10) {
                    text = '<a href="#" onclick="RejectOrder()" class="liji">退款退货</a><a href="#" onclick="ConfirmReceiptOrder(\'' + orderId+'\')" class="qu">确认收货</a>';
                }
                if (state == 15) {
                    text = '';
                }
                if (state == 20) {
                    text = text = '<a href="/Order/ProductComment?orderId=' + orderId+'"   class="lip">立即评价</a>';;
                }
                if (state == 30) {
                    text = text = '<a href="/Order/ProductComment?orderId=' + orderId + '"   class="lip">查看评价</a>';;
                }
                return text;
            }

            function BuildDIViitem(data) {
                var div = '<div class="on_d" id="div_' + data.OrderID+'">' +
                    ' <p class="bh" style="padding-left:0;padding-right:0">订单:<a href="/Order/PayOrderDetail?orderId=' + data.OrderID + '">' + data.OrderCode + '</a>'
                    + OrderMoneyStr(data)
                    +'<span style="font-size:1em">'+ GetStatusText(data.OrderState) + '</span></p>' +
                    '<div class="sp_pr">' +
                    '<a href="/shop/ProductDetail?productId=' + data.ProductId + '">' +
                    '<img src="' +data.Domain+ data.ProductImg + '">' +
                    ' <div class="text_p" style="float:none">' +
                    '<p class=" textover">' + data.ProductName + '</p>' +
                    '<h6 class="smallfont textover">' + data.ShortDesc + '</h6>' +
                    '<h6 class="smallfont textover">' + data.AllSpec + '</h6>' +
                    '<div class="xing">' +
                    priceStr(data)+
                    ' </div>' +
                    '<span class="yue" style="margin-top:1.286em;">下单时间：' + data.StrCreateTime + '</span>' +
                    '</div>' +
                    '</a>' +
                    '</div>' +
                    ' <div class="button">' + BuildOper(data.OrderID, data.OrderState, data.HasKanJia, data.KanJiaID, data.StrAleradKanJiaMoney, data.PromotionID) +
                    ' </div>' +
                    '</div>';
                return div;
            }

            function priceStr(data) {
                var text = '<span>&nbsp;&nbsp;￥<em>' + data.StrSalePrice + '</em><em> X ' + data.ProductNum +'</em></span>';
                return text;
            }

            function OrderMoneyStr(data) {
                var text = '&nbsp;&nbsp;<label class="redClass">' + data.OrderOrginPrice + '</label>';
                if (data.HasKanJia) {
                    text = '&nbsp;&nbsp<label class="midline redClass">￥<em>' + data.OrderOrginPrice + '</em></label>' + '<label class="redClass">￥<em>' + data.OrderPriceAfterKJ + '</em></label>';
                }
                return text;
            }

           function addKanJiaFromShopCar(orderId) {
               layer.open({ type: 2, shadeClose: false });
              $.ajax({
            type: "POST",
            url: '@(Domain)/Promotions/addKanJiaFromShopCar',
            data: { orderId: orderId },
            cache: false,
            success: function (data) {

                layer.open({
                    content: data.Data
                    , skin: 'msg'
                    , time: 2 //2秒后自动关闭
                    , end: function () {
                        layer.closeAll('dialog');
                    }
                });
                if (data.Success == true) {
                    window.location.href = "@(Domain + "/Promotions/KJPromotions?KanJiaID=")" + data.KanJiaId  ;
                }
            },
            error: function (data) {
            }
        });
            }

           function DelThisOrder(orderId) {

                layer.open({
                    content: '您确定要删除此此订单吗？'
                    , btn: ['确定', '不要']
                    , yes: function (index) {
                        layer.open({ type: 2, shadeClose: false });
                         $.ajax({
                            type: "post",
                            url: "@(ViewBag.DomainUrl + "/Order/DelThisOrder")",
                            dataType: "json",
                            data: { orderId: orderId },
                            success: function (data) {
                                if (data.Success == true) {
                                    layer.closeAll('dialog');
                                    $("#div_" + orderId).remove();
                                    layer.open({
                                        content: data.Data
                                        , skin: 'msg'
                                        , time: 2 //2秒后自动关闭
                                        , end: function () {
                                            layer.closeAll('dialog');
                                        }
                                    });
                                    $("#div_" + orderId).remove();
                                } else {
                                    layer.open({
                                        content: data.Data
                                        , skin: 'msg'
                                        , time: 2 //2秒后自动关闭
                                        , end: function () {
                                            layer.closeAll('dialog');
                                        }
                                    });
                                    layer.closeAll('dialog');
                                }
                                }
                        })
                    }
                });
           }

        function RejectOrder() {
            layer.open({
                content: '暂不支持系统直接退货，请直接在公众号对话框联系客服'
                , btn: '我知道了'
            });
        }

        function ConfirmReceiptOrder(orderId) {

                layer.open({
                    content: '请您确保货物完好无损后再确认收货'
                    , btn: ['确定', '取消']
                    , yes: function (index) {
                        layer.open({ type: 2, shadeClose: false });
                         $.ajax({
                            type: "post",
                            url: "@(ViewBag.DomainUrl + "/Order/ConfrimReceipeOrder")",
                            dataType: "json",
                            data: { orderId: orderId },
                            success: function (data) {
                                if (data.Success == true) {
                                    layer.closeAll('dialog');

                                    layer.open({
                                        content: data.Data
                                        , skin: 'msg'
                                        , time: 2 //2秒后自动关闭
                                        , end: function () {
                                            layer.closeAll('dialog');
                                        }
                                    });
                                    $("#div_" + orderId).remove();
                                } else {
                                    layer.open({
                                        content: data.Data
                                        , skin: 'msg'
                                        , time: 2 //2秒后自动关闭
                                        , end: function () {
                                            layer.closeAll('dialog');
                                        }
                                    });
                                    layer.closeAll('dialog');
                                }
                                }
                        })
                    }
                });
        }

    </script>
 